package com.example.templateapp.ui.adapter.base

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.annotation.NonNull
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.vlayout.DelegateAdapter
import java.util.*

/**
 *
 * @ProjectName:    TruckPriceChargeKotlin
 * @Package:        com.mtl.truckpricechargekotlin.adapter.base
 * @ClassName:      XDelegateAdapter
 * @Description:    java类作用描述
 * @Author:         yang yang
 * @CreateDate:     2020/5/7 0007 14:16
 * @UpdateUser:     更新者
 * @UpdateDate:     2020/5/7 0007 14:16
 * @UpdateRemark:   更新说明：
 * @Version:        1.0
 */
abstract class XDelegateAdapter<T,V : RecyclerView.ViewHolder> : DelegateAdapter.Adapter<V> {

    private val mData : MutableList<T> = ArrayList()

    private var mSelectPosition : Int = -1

    constructor(){}

    constructor(list:Collection<T>){
        mData.addAll(list)
    }

    constructor(data:Array<T>){
        if (data.isNotEmpty()){
            mData.addAll(data)
        }
    }

    protected abstract fun getViewHolder(@NonNull parent:ViewGroup, viewType:Int):V

    protected abstract fun bindData(@NonNull holder:V, position: Int, item: T)

    protected fun inflateView(parent:ViewGroup,@LayoutRes layoutId: Int): View {
        return LayoutInflater.from(parent.context).inflate(layoutId,parent,false)
    }

    override fun onCreateViewHolder(parent: ViewGroup, layoutId: Int): V {
        return getViewHolder(parent, layoutId)
    }

    override fun onBindViewHolder(holder: V, position: Int) {
        bindData(holder, position, mData[position])
    }

    /**
     * 获取列表项
     *
     * @param position
     * @return
     */
    open fun getItem(position: Int): T? {
        return if (checkPosition(position)) mData[position] else null
    }

    private fun checkPosition(position: Int): Boolean {
        return position >= 0 && position <= mData.size - 1
    }

    open fun isEmpty(): Boolean {
        return itemCount == 0
    }

    override fun getItemCount(): Int {
        return mData.size
    }

    /**
     * @return 数据源
     */
    open fun getData(): List<T>? {
        return mData
    }

    /**
     * 给指定位置添加一项
     *
     * @param pos
     * @param item
     * @return
     */
    open fun add(pos: Int, item: T): XDelegateAdapter<T,V>? {
        mData.add(pos, item)
        notifyItemInserted(pos)
        return this
    }

    /**
     * 在列表末端增加一项
     *
     * @param item
     * @return
     */
    open fun add(item: T): XDelegateAdapter<T,V>? {
        mData.add(item)
        notifyItemInserted(mData.size - 1)
        return this
    }

    /**
     * 删除列表中指定索引的数据
     *
     * @param pos
     * @return
     */
    open fun delete(pos: Int): XDelegateAdapter<T,V>? {
        mData.removeAt(pos)
        notifyItemRemoved(pos)
        return this
    }

    /**
     * 刷新列表中指定位置的数据
     *
     * @param pos
     * @param item
     * @return
     */
    open fun refresh(pos: Int, item: T): XDelegateAdapter<T,V>? {
        mData[pos] = item
        notifyItemChanged(pos)
        return this
    }

    /**
     * 刷新列表数据
     *
     * @param collection
     * @return
     */
    open fun refresh(collection: Collection<T>?): XDelegateAdapter<T,V>? {
        if (collection != null) {
            mData.clear()
            mData.addAll(collection)
            mSelectPosition = -1
            notifyDataSetChanged()
        }
        return this
    }

    /**
     * 刷新列表数据
     *
     * @param array
     * @return
     */
    open fun refresh(array: Array<T>?): XDelegateAdapter<T,V>? {
        if (array != null && array.size > 0) {
            mData.clear()
            mData.addAll(Arrays.asList(*array))
            mSelectPosition = -1
            notifyDataSetChanged()
        }
        return this
    }

    /**
     * 加载更多
     *
     * @param collection
     * @return
     */
    open fun loadMore(collection: Collection<T>?): XDelegateAdapter<T,V>? {
        if (collection != null) {
            mData.addAll(collection)
            notifyDataSetChanged()
        }
        return this
    }

    /**
     * 加载更多
     *
     * @param array
     * @return
     */
    open fun loadMore(array: Array<T>?): XDelegateAdapter<T,V>? {
        if (array != null && array.size > 0) {
            mData.addAll(Arrays.asList(*array))
            notifyDataSetChanged()
        }
        return this
    }

    /**
     * 添加一个
     *
     * @param item
     * @return
     */
    open fun load(item: T?): XDelegateAdapter<T,V>? {
        if (item != null) {
            mData.add(item)
            notifyDataSetChanged()
        }
        return this
    }

    /**
     * @return 当前列表的选中项
     */
    open fun getSelectPosition(): Int {
        return mSelectPosition
    }

    /**
     * 设置当前列表的选中项
     *
     * @param selectPosition
     * @return
     */
    open fun setSelectPosition(selectPosition: Int): XDelegateAdapter<T,V>? {
        mSelectPosition = selectPosition
        notifyDataSetChanged()
        return this
    }

    /**
     * 获取当前列表选中项
     *
     * @return 当前列表选中项
     */
    open fun getSelectItem(): T? {
        return getItem(mSelectPosition)
    }

    /**
     * 清除数据
     */
    open fun clear() {
        if (!isEmpty()) {
            mData.clear()
            mSelectPosition = -1
            notifyDataSetChanged()
        }
    }

}